<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货号" prop="itemNo">
        <el-input
          v-model="queryParams.itemNo"
          placeholder="请输入货号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="lotNo">
        <el-input
          v-model="queryParams.lotNo"
          placeholder="请输入批号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="条形码" prop="barcode">
        <el-input
          v-model="queryParams.barcode"
          placeholder="请输入条形码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="打印状态" prop="printState">-->
      <!--        <el-select v-model="queryParams.printState" placeholder="请选择打印状态" clearable size="small">-->
      <!--          <el-option-->
      <!--            v-for="dict in printStateOptions"-->
      <!--            :key="dict.dictValue"-->
      <!--            :label="dict.dictLabel"-->
      <!--            :value="dict.dictValue"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-toilet-paper"
          size="mini"
          :disabled="multiple"
          @click="handleReCreateCode"
          v-hasPermi="['product:reagentBoxBac:reprint']"
        >重打标签
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:reagentBoxBac:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="reagentBoxList" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="订单ID" align="center" prop="orderId" width="60"/>
      <el-table-column label="品名" align="center" prop="projectCode" width="70"/>
      <el-table-column label="机型" align="center" prop="machineType" width="90"/>
      <el-table-column label="货号" align="center" prop="itemNo" width="70"/>
      <el-table-column label="规格" align="center" prop="reagentSpec"/>
      <el-table-column label="有效期" align="center" prop="expireDate" width="100"/>
      <el-table-column label="批号" align="center" prop="lotNo" width="100"/>
      <el-table-column sortable label="条码" align="center" prop="barcode" width="150"/>
      <el-table-column label="注册证号" align="center" prop="registrationNo"/>
      <el-table-column label="生产许可证号" align="center" prop="productionLicenseNo"/>
      <el-table-column label="样张" align="center" prop="reserveFlag" width="60">
        <template slot-scope="scope">
          <el-tag :type="scope.row.reserveFlag ?'primary':'info'"
          >{{ scope.row.reserveFlag ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column label="打印状态" align="center" prop="printState" width="80" :formatter="printStateFormat">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-tag-->
      <!--            :type="scope.row.checkState === '1' ? 'success' : 'primary'"-->
      <!--            disable-transitions>{{ printStateFormat(scope.row) }}-->
      <!--          </el-tag>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-toilet-paper"
            @click="handleReCreateCode(scope.row)"
            v-hasPermi="['product:reagentBoxBac:reprint']"
          >重打标签
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  addReagentBoxBac,
  delReagentBoxBac,
  exportReagentBoxBac,
  getReagentBoxBac,
  listReagentBoxBac,
  updateReagentBoxBac
} from "@/api/product/reagentBoxBac";
import {addReagentBox} from "@/api/product/reagentBox";

export default {
  name: "ReagentBox",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 试剂盒记录表格数据
      reagentBoxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 打印状态字典
      printStateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderListId: undefined,
        orderId: undefined,
        projectCode: undefined,
        projectName: undefined,
        testMethod: undefined,
        itemNo: undefined,
        reagentSpec: undefined,
        expireDate: undefined,
        lotNo: undefined,
        barcode: undefined,
        registrationNo: undefined,
        productionLicenseNo: undefined,
        printState: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderListId: [
          {required: true, message: "订单详细ID不能为空", trigger: "blur"}
        ],
        orderId: [
          {required: true, message: "订单ID不能为空", trigger: "blur"}
        ],
        projectCode: [
          {required: true, message: "品名不能为空", trigger: "blur"}
        ],
        projectName: [
          {required: true, message: "中文名称不能为空", trigger: "blur"}
        ],
        testMethod: [
          {required: true, message: "方法学不能为空", trigger: "blur"}
        ],
        itemNo: [
          {required: true, message: "货号不能为空", trigger: "blur"}
        ],
        reagentSpec: [
          {required: true, message: "规格不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "配制日期不能为空", trigger: "blur"}
        ],
        expireDate: [
          {required: true, message: "有效期不能为空", trigger: "blur"}
        ],
        lotNo: [
          {required: true, message: "批号不能为空", trigger: "blur"}
        ],
        barcode: [
          {required: true, message: "条形码不能为空", trigger: "blur"}
        ],
        registrationNo: [
          {required: true, message: "注册证号不能为空", trigger: "blur"}
        ],
        productionLicenseNo: [
          {required: true, message: "生产许可证号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("reagent_barcode_print_state").then(response => {
      this.printStateOptions = response.data;
    });
  },
  methods: {
    /** 查询试剂盒记录列表 */
    getList() {
      this.loading = true;
      listReagentBoxBac(this.queryParams).then(response => {
        this.reagentBoxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 打印状态字典翻译
    printStateFormat(row, column) {
      return this.selectDictLabel(this.printStateOptions, row.printState);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        boxId: undefined,
        orderListId: undefined,
        orderId: undefined,
        projectCode: undefined,
        projectName: undefined,
        testMethod: undefined,
        itemNo: undefined,
        reagentSpec: undefined,
        createTime: undefined,
        expireDate: undefined,
        lotNo: undefined,
        barcode: undefined,
        registrationNo: undefined,
        productionLicenseNo: undefined,
        printState: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加试剂盒记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const boxs = row || this.ids
      getReagentBoxBac(boxs.boxId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改试剂盒记录";
      });
    },
    //重打标签
    handleReCreateCode(row) {
      const boxs = row.boxId ? row : this.ids
      if (Array.isArray(boxs)) {
        for (let box of boxs) {
          addReagentBox(box).then(response => {
            if (response.code === 200) {
              delReagentBoxBac(box.boxId)
              this.msgSuccess("盒贴编号" + box.boxId + "提交成功");
              this.open = false;
              this.getList();
            }
          });
        }
      } else {
        addReagentBox(boxs).then(response => {
          if (response.code === 200) {
            delReagentBoxBac(boxs.boxId).then(res => {
              this.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            })
          }
        });
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.boxId != undefined) {
            updateReagentBoxBac(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addReagentBoxBac(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const boxIds = row.boxId || this.ids;
      this.$confirm('是否确认删除试剂盒记录编号为"' + boxIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delReagentBoxBac(boxIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有试剂盒记录数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportReagentBoxBac(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () {
      });
    }
  }
};
</script>
